package cn.com.ddapp.ddm.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.zhy.autolayout.AutoLinearLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.bean.mine.AliPay;
import cn.com.ddapp.ddm.ui.mine.BindAlipayActivity;
import cn.com.ddapp.ddm.util.TDevice;

/**
 * Created by admin on 2016/8/29.
 */
public class AlipayListAdapter extends BaseAdapter {
    private Context context;
    private List<AliPay.ListBean> list;
    private int start = -1;
    private int close = -1;
    private boolean all = true;
    int orientation;
    int height;

    public AlipayListAdapter(Context context, List<AliPay.ListBean> list, int orientation) {
        this.context = context;
        this.list = list;
        this.orientation = orientation;
        height = (int)(TDevice.getScreenHeight()/5);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == list.size() - 1) {
            return 0;
        } else {
            return 1;
        }
    }

    //某一条改变背景
    public void setMySoothAlterPosit(int index) {
        this.start = index;
        notifyDataSetChanged();
    }

    //某一条恢复原状
    public void setMySoothClosePosit(int index) {
        this.close = index;
        notifyDataSetChanged();
    }

    public void setOld(boolean all) {
        this.all = all;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        ViewHolder1 holder1 = null;
        int type = getItemViewType(i);
        if (convertView == null) {
            switch (type) {
                //显示添加
                case 0:
                    convertView = LayoutInflater.from(context).inflate(R.layout.item_add_btn3, parent, false);
                    holder = new ViewHolder(convertView);
                    convertView.setTag(holder);
                    break;
                //显示内容
                case 1:
                    convertView = LayoutInflater.from(context).inflate(R.layout.alipay_listview_item, parent, false);
                    holder1 = new ViewHolder1(convertView);
                    convertView.setTag(holder1);
                    break;
            }
        } else {
            switch (type) {
                //显示添加
                case 0:
                    holder = (ViewHolder) convertView.getTag();
                    break;
                //显示内容
                case 1:
                    holder1 = (ViewHolder1) convertView.getTag();
                    break;
            }
        }
        switch (type) {
            //显示添加
            case 0:
                convertView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(context, BindAlipayActivity.class);
                        context.startActivity(intent);
                    }
                });
                break;
            //显示内容
            case 1:
                if (orientation == 0 || orientation == 2) {
                    if (i == start) {
                        convertView.setBackgroundResource(R.mipmap.zhifub2);
                    }
                    if (i == close) {
                        convertView.setBackgroundResource(R.mipmap.zhifub1);
                    }
                    if (all) {
                        for (int j = 0; j < list.size(); j++) {
                            convertView.setBackgroundResource(R.mipmap.zhifub1);
                        }
                    }
                    //convertView.setBackgroundResource(R.mipmap.zhifub1);
                    //holder1.alipaylayout.getLayoutParams().height = height;
                    //convertView.getLayoutParams().height = height;
                    //Log.e("11111","height"+orientation);
                } else if (orientation == 1) {
                    if (i == start) {
                        convertView.setBackgroundResource(R.mipmap.zhifubao2);
                    }
                    if (i == close) {
                        //有圆角
                        convertView.setBackgroundResource(R.mipmap.zhifubao1);
                    }
                    if (all) {
                        for (int j = 0; j < list.size(); j++) {
                            convertView.setBackgroundResource(R.mipmap.zhifubao1);
                        }
                    }
                }

                String a = list.get(i).getBankAccount();
                String b = a.substring(a.length() - 4, a.length());
                holder1.alipayAccount.setText("*** **** " + b);
                break;
        }
        return convertView;
    }

    class ViewHolder1 {
        @BindView(R.id.alipay_account)
        TextView alipayAccount;
        @BindView(R.id.alipaylayout)
        AutoLinearLayout alipaylayout;

        ViewHolder1(View view) {
            ButterKnife.bind(this, view);
        }
    }

    static class ViewHolder {
        @BindView(R.id.add_alipay)
        TextView addAlipay;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

}
